package com.topfinance.ui;

import java.util.List;

import com.topfinance.frame.exception.AppException;
import com.topfinance.payment.dto.*;
import com.topfinance.payment.ebo.*;
import com.topfinance.payment.service.*;
import com.topfinance.payment.service.impl.*;

public class UICfgHelper extends UIHelper{
	private static UICfgHelper instance;
	private UICfgHelper() {
		init();
	}
	
	public static UICfgHelper getInstance() {
		if (instance == null) {
			instance = new UICfgHelper();
		}
		return instance;
	}
	
	/**
	 * 保存配置业务对象，不限对象类型
	 * 
	 * @return 配置业务对象
	 */
	public Object saveBizObj(Object obj) throws AppException {
		ITCfgProtocolMgr impl = (TCfgProtocolMgrImpl) f3.getBean("cfgProtocolMgr");
		// 增加新的业务对象
		impl.save(obj);
		return obj;
	}

	/**
	 * 更新配置业务对象，不限对象类型
	 * 
	 * @return 配置业务对象
	 */
	public Object updateBizObj(Object obj) throws AppException {
		ITCfgProtocolMgr impl = (TCfgProtocolMgrImpl) f3.getBean("cfgProtocolMgr");
		// 更新业务对象
		impl.update(obj);
		return obj;
	}

	/**
	 * 删除配置业务对象，不限对象类型
	 * 
	 * @return 无
	 */
	public void delBizObj(Object obj) throws AppException {
		ITCfgProtocolMgr impl = (TCfgProtocolMgrImpl) f3.getBean("cfgProtocolMgr");
		// 增加新的业务对象
		impl.remove(obj);
	}
	
	/**
	 * 返回所有的支付报文协议定义类型
	 * 
	 * @return协议列表
	 */
	public List<TCfgProtocolEbo> listPrtcl() throws AppException {
		ITCfgProtocolMgr prtclImpl = (TCfgProtocolMgrImpl) f3.getBean("cfgProtocolMgr");
		List<TCfgProtocolEbo> prtcls = prtclImpl.getAllPrtcl();
		return prtcls;
	}

	/**
	 * 返回所有的节点定义类型
	 * 
	 * @return节点列表
	 */
	public List<TCfgNodeEbo> listNode() throws AppException {
		ITCfgNodeMgr nodeImp = (TCfgNodeMgrImpl) f3.getBean("cfgNodeMgr");
		List<TCfgNodeEbo> nodes = nodeImp.getAllNode();
		return nodes;
	}

	/**
	 * 返回所有的TransOut类型
	 * 
	 * @return TransOut列表
	 */
	public List<TCfgTransOutEbo> listTransOut() throws AppException {
		ITCfgTransOutMgr toImp = (TCfgTransOutMgrImpl) f3.getBean("cfgTransOutMgr");
		List<TCfgTransOutEbo> tos = toImp.getAllTransOut();
		return tos;
	}

	/**
	 * 保存或者更新TransOutEbo和TCfgConfigSetDto列表
	 * 如果要保持新的TransOutEbo，请调用此方法
	 * @return TransOutEbo业务对象
	 */
	public TCfgTransOutEbo saveTransIn(TCfgTransOutEbo to,List<TCfgConfigSetDto> sets) throws AppException {
		TCfgTransOutMgrImpl toImp = (TCfgTransOutMgrImpl) f3.getBean("cfgTransOutMgr");
		String configs = "";
		for(int i=0;i<sets.size();i++){
			configs += sets.get(i);
			if(i!=sets.size())
				configs+=",";
		}
		to.setConfig(configs);
		toImp.save(to);

		for(TCfgConfigSetDto set:sets){
			TCfgConfigEbo cfg = null;
			if(set.getId() != null)
				cfg = toImp.get(TCfgConfigEbo.class,set.getId());
			else
				cfg = new TCfgConfigEbo();
			cfg.setCfgName(set.getKey());
			cfg.setCfgValue(set.getValue());
			cfg.setCfgType(to.getProvider());
			cfg.setPoid(to.getOid());
			toImp.saveOrUpdate(cfg);
		}
		return to;
	}
	
	/**
	 * 返回所有的TransIn类型
	 * 
	 * @return TransIn列表
	 */
	public List<TCfgTransInEbo> listTransIn() throws AppException {
		ITCfgTransInMgr tiImp = (TCfgTransInMgrImpl) f3.getBean("cfgTransInMgr");
		List<TCfgTransInEbo> tis = tiImp.getAllTransIn();
		return tis;
	}

	/**
	 * 保存或者更新TransInEbo和TCfgConfigSetDto列表
	 * 如果要保持新的TransInEbo，请调用此方法
	 * @return TransInEbo业务对象
	 */
	public TCfgTransInEbo saveTransIn(TCfgTransInEbo ti,List<TCfgConfigSetDto> sets) throws AppException {
		TCfgTransInMgrImpl tiImp = (TCfgTransInMgrImpl) f3.getBean("cfgTransInMgr");
		String configs = "";
		for(int i=0;i<sets.size();i++){
			configs += sets.get(i);
			if(i!=sets.size()-1)
				configs+=",";
		}
		ti.setConfig(configs);
		tiImp.save(ti);
		for(TCfgConfigSetDto set:sets){
			TCfgConfigEbo cfg = null;
			if(set.getId() != null)
				cfg = tiImp.get(TCfgConfigEbo.class,set.getId());
			else
				cfg = new TCfgConfigEbo();
			cfg.setCfgName(set.getKey());
			cfg.setCfgValue(set.getValue());
			cfg.setCfgType(ti.getProvider());
			cfg.setPoid(ti.getOid());
			tiImp.saveOrUpdate(cfg);
		}
		return ti;
	}
	
	/**
	 * 返回所有的PortOut类型
	 * 
	 * @return PortOut列表
	 */
	public List<TCfgPortOutEbo> listPortOut() throws AppException {
		ITCfgPortOutMgr poImp = (TCfgPortOutMgrImpl) f3.getBean("cfgPortOutMgr");
		List<TCfgPortOutEbo> pos = poImp.getAllPortOut();
		return pos;
	}
	
	/**
	 * 保存PortInDTO业务对象
	 * 
	 * @return InPort业务对象
	 */
	public TCfgPortOutEbo savePortOutDto(TCfgOutPortDto portDto) throws AppException {
		ITCfgPortOutMgr poImp = (TCfgPortOutMgrImpl) f3.getBean("cfgPortOutMgr");
		TCfgPortOutEbo o = poImp.addOutPort(portDto);
		// 增加新的OutPort业务对象
		return o;
	}
	
	/**
	 * 返回所有的PortIn类型
	 * 
	 * @return PortIn列表
	 */
	public List<TCfgPortInEbo> listPortIn() throws AppException {
		ITCfgPortInMgr piImp = (TCfgPortInMgrImpl) f3.getBean("cfgPortInMgr");
		List<TCfgPortInEbo> pis = piImp.getAllPortIn();
		return pis;
	}

	/**
	 * 保存PortInDTO业务对象
	 * 
	 * @return InPort业务对象
	 */
	public TCfgPortInEbo savePortInDto(TCfgInPortDto portDto) throws AppException {
		ITCfgPortInMgr piImp = (TCfgPortInMgrImpl) f3.getBean("cfgPortInMgr");
		TCfgPortInEbo o = piImp.addInPort(portDto);
		// 增加新的InPort业务对象
		return o;
	}

	/**
	 * 返回所有的Operation类型
	 * 
	 * @return Operation列表
	 */
	public List<TCfgOperationEbo> listOper() throws AppException {
		ITCfgOperationMgr operImp = (TCfgOperationMgrImpl) f3.getBean("cfgOperationMgr");
		List<TCfgOperationEbo> opers = operImp.getAllOper();
		return opers;
	}

	/**
	 * 保存OperDTO业务对象
	 * 
	 * @return TCfgOperationEbo业务对象
	 */
	public TCfgOperationEbo savePortInDto(TCfgOperationDto operDto) throws AppException {
		ITCfgOperationMgr operImp = (TCfgOperationMgrImpl) f3.getBean("cfgOperationMgr");
		TCfgOperationEbo o = operImp.addOper(operDto);
		// 增加新的OperPort业务对象
		return o;
	}
	
	/**
	 * 返回所有的RouteRule类型
	 * 
	 * @return RouteRule列表
	 */
	public List<TCfgRouteRuleEbo> listRouteRule() throws AppException {
		 ITCfgRouteRuleMgr ruleImp = (TCfgRouteRuleMgrImpl) f3.getBean("cfgRouteRuleMgr");
		 List<TCfgRouteRuleEbo> rules = ruleImp.getAllRouteRule();
		return rules;
	}
	
	/**
	 * 保存OperDTO业务对象
	 * 
	 * @return TCfgOperationEbo业务对象
	 */
	public TCfgRouteRuleEbo saveRouteRuleDto(TCfgRouteRuleDto ruleDto) throws AppException {
		ITCfgRouteRuleMgr ruleImp = (TCfgRouteRuleMgrImpl) f3.getBean("cfgRouteRuleMgr");
		TCfgRouteRuleEbo o = ruleImp.addRoute(ruleDto);
		// 增加新的TCfgRouteRuleEbo业务对象
		return o;
	}

	/**
	 * 返回与当前对象类型匹配的ConfigSet设置
	 * 输入：已有对象的对象OID，选择的类型
	 * @return RouteRule列表
	 */
	public List<TCfgConfigSetDto> listCfgSet(Integer poid,String provider) throws AppException {
		ITCfgConfigSetMgr csImp = (TCfgConfigSetMgrImpl) f3.getBean("cfgConfigSetMgr");
		List<TCfgConfigSetDto> css = csImp.getDefCfgSet(poid,provider);
		return css;
	}
	
	//根据报文号、业务类型号、业务种类号条件获得映射定义
	//一直调用这个方法来保持映射定义
	public TCfgFmtEleMapFileEbo saveFmtEleMap(Integer fmtID,String msg,String tpCode,String clsCode) throws AppException{
		//在同一组条件下只保留一组
		ITCfgFmtEleMapMgr mapImp = (TCfgFmtEleMapMgrImpl) f3.getBean("cfgEleMapMgr");
		List<TCfgFmtEleMapFileEbo> maps = mapImp.getAllFmtEleMap(fmtID, msg, tpCode, clsCode);
		if(maps.size()==1)
			return maps.get(0);
		
		TCfgFormatEbo fmt = (TCfgFormatEbo)get(TCfgFormatEbo.class,fmtID);
		TCfgFmtEleMapFileEbo map = new TCfgFmtEleMapFileEbo();
		map.setMsgCode("HVPS.111.001.01");
		map.setTpCode("A100");
		map.setClsCode("02101");
		map.setTCfgFormat(fmt);
		saveBizObj(map);
		return map;
	}

	//根据报文号、业务类型号、业务种类号条件映射定义
	public List<TCfgFmtEleMapFileEbo> getAllFmtEleMap(Integer fmtID,String msg,String tpCode,String clsCode) throws AppException{
		ITCfgFmtEleMapMgr mapImp = (TCfgFmtEleMapMgrImpl) f3.getBean("cfgEleMapMgr");
		List<TCfgFmtEleMapFileEbo> maps = mapImp.getAllFmtEleMap(fmtID, msg, tpCode, clsCode);
		return maps;
	}
	
	//获得位置对应序列号
	//fmtOId=format对应的主键，pos=位置名称
	public String getPosSequn(Integer fmtOId,String pos) throws AppException{
		ITCfgFormatMgr fmtImp = (TCfgFormatMgrImpl) f3.getBean("cfgFormatMgr");
		return fmtImp.getPosSequn(fmtOId, pos);		
	}
	
	public static void main(String[] args) {
		try {
			UICfgHelper helper = UICfgHelper.getInstance();
			//测试TransIn/TransOut及ConfigSet
//			List<TCfgConfigSetDto> css = helper.listCfgSet(new Integer(1340),"IBMMQ");
//			for(TCfgConfigSetDto c:css){
//				System.out.println(c);
//			}
			
			System.out.println(helper.getPosSequn(new Integer(2),"S5811"));
			
			//测试Prtcl配置
			List<TCfgProtocolEbo> prtcls = helper.listPrtcl();
			for(TCfgProtocolEbo p:prtcls){
				System.out.println("[Name]" + p.getName());
			}
			
//			TCfgTransInEbo ti = new TCfgTransInEbo();
//			ti.setName("Test_TO");
//			ti.setPrefix("PRE");
//			ti.setProvider("IBMMQ");
//			ti.setIsprivate("F");
//			helper.saveTransIn(ti, css);
			
			//测试映射配置
//			TCfgFmtEleMapFileEbo map = helper.saveFmtEleMap(new Integer(2),"HVPS.111.001.01","A100",null);
//			TCfgFmtEleMapRuleEbo rule = new TCfgFmtEleMapRuleEbo();
//			rule.setMapFile(map);
//			rule.setPteFldId(10);
//			rule.setPteFldPath("pteFldPath");
//			rule.setPteFldType("pteFldType");
//			rule.setBizFldEleId("bizFldEleId");
//			rule.setBizFldMode("bizFldMode");
//			rule.setBizFldName("bizFldName");
//			rule.setBizFldPath("bizFldPath");
//			rule.setBizFldType("bizFldType");
//			rule.setBizFldValue("bizFldValue");
//			helper.saveBizObj(rule);
//			List<TCfgFmtEleMapFileEbo> maps = helper.getAllFmtEleMap(null,null,null,null);
//			for(TCfgFmtEleMapFileEbo map:maps){
//				System.out.println("[Map]" + map.getFormat()+"="+map.getMsgCode()+"_"+map.getTpCode()+"_"+map.getClsCode());
//			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}
